package com.hospital.service.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.system.model.param.SysConfigurationRemoveBatchParam;
import enhance.plus.logic.LogicBaseController;
import com.hospital.service.system.entity.SysConfiguration;
import com.hospital.service.system.entity.help.SysConfigurationHelp;
import com.hospital.service.system.model.param.SysConfigurationParam;
import com.hospital.service.system.service.SysConfigurationService;
import com.hospital.service.system.model.param.SysConfigurationQueryForOneRowParam;
import com.hospital.service.system.model.param.SysConfigurationQueryPageParam;
import com.hospital.service.system.model.vo.SysConfigurationVo;
import common.core.util.CommonUtil;
import common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import enhance.plus.util.Page;
import enhance.plus.util.PageHelper;


import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 系统配置表 控制器
 *
 * @author AnKh
 * @created 2020-10-11 20:27:11
 */
@Slf4j
@Validated
@RestController
@Api(tags = "系统配置表")
@RequestMapping("/system/sysConfiguration")
public class SysConfigurationController extends LogicBaseController<SysConfigurationService, SysConfiguration> {

    @Autowired
    private SysConfigurationService sysConfigurationService;

    @ApiOperation(value = "保存系统配置表的数据")
    @RequestMapping(value = "/save" , method = RequestMethod.PUT)
    public Result save(@Valid @RequestBody SysConfigurationParam param){
        log.debug("开始保存系统配置表-->{}",param);
        return Result.success(this.sysConfigurationService.save(CommonUtil.copyBean(param,SysConfiguration.class)));
     }

    @ApiOperation(value = "更新系统配置表的数据")
    @RequestMapping(value = "/update" , method = RequestMethod.PUT)
    public Result update(@Valid @RequestBody SysConfigurationParam param){
        log.debug("开始跟新系统配置表-->{}",param);
        return Result.success(this.sysConfigurationService.updateById(CommonUtil.copyBean(param,SysConfiguration.class)));
    }

    @ApiOperation(value = "批量删除系统配置表的数据")
    @RequestMapping(value = "/removeBatch" , method = RequestMethod.DELETE)
    public Result delete(@Valid @RequestBody SysConfigurationRemoveBatchParam param){
        this.sysConfigurationService.removeBatch(param);
        return Result.success();
    }

    @ApiOperation(value = "删除系统配置表的一条数据")
    @RequestMapping(value = "/delete" , method = RequestMethod.DELETE)
    @ApiImplicitParams(
            @ApiImplicitParam(name= "id" ,value = "数据ID", required = true , example = "-1" )
    )
    public Result delete(@Valid @RequestParam @NotBlank(message = "删除数据ID不能为空") String id){
        log.debug("开始删除检测抗体表的数据-->{}",id);
        return Result.success(this.sysConfigurationService.removeById(id));
    }

    @ApiOperation(value = "根据条件查询一条系统配置表的数据")
    @RequestMapping(value = "/queryForOneRow" , method = RequestMethod.POST)
    public Result<SysConfigurationVo> queryForOneRow(@Valid @RequestBody SysConfigurationQueryForOneRowParam param){
        SysConfiguration row = this.sysConfigurationService.getOne(param.convertParamToQueryWapper());
        return Result.success( CommonUtil.copyBean(row, SysConfigurationVo.class));
    }

    @ApiOperation(value = "根据条件分页查询系统配置表的数据")
    @RequestMapping(value = "/queryPage" , method = RequestMethod.POST)
    public Result<Page<SysConfigurationVo>> queryPage(@Valid @RequestBody SysConfigurationQueryPageParam param){
        QueryWrapper<SysConfiguration> query = SysConfigurationHelp.createQuery();
        query.like(CommonUtil.isNotEmpty(param.getSysValue()), SysConfigurationHelp.sysValue, param.getSysValue());
        query.eq(CommonUtil.isNotEmpty(param.getSysGroup()), SysConfigurationHelp.sysGroup, param.getSysGroup());
        return Result.success(this.sysConfigurationService.page(param.page(), query));
    }

    @ApiOperation(value = "根据条件查询系统配置表的数据")
    @RequestMapping(value = "/queryList" , method = RequestMethod.POST)
    public Result<List<SysConfigurationVo>> queryList(@Valid @RequestBody SysConfigurationQueryPageParam param ){
        QueryWrapper<SysConfiguration> query = SysConfigurationHelp.createQuery();
        query.eq(CommonUtil.isNotEmpty(param.getSysGroup()), SysConfigurationHelp.sysGroup, param.getSysGroup());
        return Result.success(this.sysConfigurationService.list(query));
    }
}
